function [dCh,hdr]=readWalter16bit(fName,nChannels,blockSize)
%data=readWalter16bit(fName,nChannels,blocksize)
% reads Walter binary format as integer
% 
% [data hdr]=readWalter16bit(fName)
% reads Walter binary format as Voltage and header information 
%PURPOSE
%INPUT
%OUTPUT

if nargin <3,
    % read header file
    if isempty(strfind(fName,'.bin')),
        hName=[fName '.asc'];
        fName=[fName '.bin'];
    else
        hName=[fName(1:strfind(fName,'.bin')-1) '.asc'];
    end
    hH=fopen(hName);
    TLINE=0;
    lineCount=0;
    startChTable=1000;
    while ~(TLINE<0),
        TLINE=fgetl(hH);
        if isempty(TLINE),TLINE='#';end
        lineCount=lineCount+1;
        if ~isempty(strfind(TLINE,'Start Line of Channel Table')),
            startChTable = str2double(TLINE(1:strfind(TLINE,'Start Line of Channel Table')-3));
        end
        if ~isempty(strfind(TLINE,'Sampling Interval (usec)')),
            hdr.Fs = 1e6/str2double(TLINE(1:strfind(TLINE,'Sampling Interval (usec)')-3));
        end
        if ~isempty(strfind(TLINE,'Segment Length')),
            blockSize = str2double(TLINE(1:strfind(TLINE,'Segment Length')-3));
        end
        if ~isempty(strfind(TLINE,'Number of Channels')),
            nChannels = str2double(TLINE(1:strfind(TLINE,'Number of Channels')-3));
            hdr.label=cell(nChannels,1);
            VpBit = zeros(nChannels,1);
        end
        if lineCount>=startChTable && lineCount<startChTable+nChannels,
            tmp=strfind(TLINE,' ');
            hdr.label{lineCount-startChTable+1}=TLINE(1:tmp(1)-1);
            VpBit(lineCount-startChTable+1)=1e-9*str2double(TLINE(14:end));
        end
    end
    fclose(hH);
else
    VpBit=ones(nChannels,1);
end

fH=fopen(fName);
data=fread(fH,'int16');
fclose(fH);
nSamp=length(data);
data=reshape(data,blockSize,nSamp/blockSize);
nSampNew=length(data(1,1:nChannels:end))*blockSize;
dCh = zeros(nChannels,nSampNew);
for k=1:nChannels
    %pick the channels out
    tmp=data(:,k:nChannels:end);
    dCh(k,:)=tmp(:)*VpBit(k);
end
